from tkinter import *
from tkinter import font
from tkcalendar import DateEntry
from datetime import date
from datetime import timedelta
from win10toast import ToastNotifier  
import apod_object_parser
import wallpaper_utility
import threading
import stony


def setWallpaperByDate(cal):
    disableButtons()
    response = apod_object_parser.get_data_by_date( wallpaper_utility.APOD_API_KEY, cal.get_date() )
    hd_url = setResultAndGetHdUrl(response)
    image_date = apod_object_parser.get_date(response)
    setWallpaperByHdUrl(hd_url, image_date)
    enableButtons()
    exit()

def setWallpaperByDateThreaded(cal):
    print("那天盾铁漂流到了宇宙哪里？")
    e = threading.Event()

    t = threading.Thread(target=setWallpaperByDate, args=(cal,))
    t.start()

def setTodaysPicAsWalpaper():
    disableButtons()
    response = apod_object_parser.get_data(wallpaper_utility.APOD_API_KEY)
    hd_url = setResultAndGetHdUrl(response)
    image_date = apod_object_parser.get_date(response)
    setWallpaperByHdUrl(hd_url, image_date)
    enableButtons()
    exit()


def setTodaysPicAsWalpaperThreaded():
    e = threading.Event()
    t = threading.Thread(target=setTodaysPicAsWalpaper)
    t.start()


def setResultAndGetHdUrl(response):
    try:
        print("发生请求中")
        hd_url = apod_object_parser.get_hdurl(response)
        print("请求已成功！")
        result.set("照片正在传输：）")
        return hd_url
    except:
        result.set("哦不！那天没有图片")
        return None


def setWallpaperByHdUrl(hd_url, image_date):
    print(" hd_url's image date: "+image_date)
    if (hd_url != None):
        image_downloaded_path = apod_object_parser.download_image(hd_url, image_date)
        resized_img1=stony.mix(image_downloaded_path)
        saved_image_path = apod_object_parser.save_image(resized_img1, image_date)
        wallpaper_utility.changeBG(saved_image_path)
        result.set("成功啦:)")
        n.show_toast(wallpaper_utility.SERVICE_NAME, "漫游图片已送达", duration = 7,)

def disableButtons():
    setButton["state"] = "disabled"
    useTodaysPic["state"] = "disabled"

def enableButtons():
    setButton["state"] = "normal"
    useTodaysPic["state"] = "normal"

root = Tk()
custom_font = font.Font(family="微软雅黑", size=12, weight="bold")

root.title("银河系盾铁漫游指南")
root.configure(bg='White',relief="flat")

n = ToastNotifier()

result = StringVar()
path = StringVar()

yesterdayDate = date.today() - timedelta(days=1)
cal = DateEntry(root, maxdate = yesterdayDate, date_pattern='dd/mm/yyyy', font=custom_font, relief="flat", fg="#77431E")
cal.grid(row = 0, column = 2, columnspan=2, rowspan=2, padx=5, pady=5,)
 
setButton = Button(root, text="确认", command=lambda: setWallpaperByDateThreaded(cal),fg="#77431E", font=custom_font, relief="flat")
setButton.grid(row=2, column=0, columnspan=2, rowspan=2, padx=5, pady=5,)

useTodaysPic = Button(root, text="今天", command= setTodaysPicAsWalpaperThreaded,fg="#77431E" ,font=custom_font, relief="flat")
useTodaysPic.grid(row=4, column=0, columnspan=2, rowspan=2, padx=5, pady=5,)

Label( root, text = "选择独特的那天 :", fg="#77431E",  font=custom_font,relief="flat").grid(row=0, sticky=W)


mainloop()